﻿using System;
using System.Collections;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common.Lists;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Profile;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security;
using DotNetNuke.Security.Profile;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Skins.Controls;
using DotNetNuke.UI.Utilities;
using DotNetNuke.UI.WebControls;
using DotNetNuke.Framework;

//using DotNetNuke.Entities.Modules.Actions;
//using DotNetNuke.Services.Localization;
//using DotNetNuke.Security;
//using DotNetNuke.UI.WebControls;
//using DotNetNuke.Services.Exceptions;

using BuNe = VLUNewsBusiness.VLUNews;
using BuUt = VLUNewsBusiness.Utilities;
using System.Collections.Generic;

namespace VLUNewsUI.VLUNews
{
    public partial class HtmlContents : PortalModuleBase
    {
        #region Private Members
        private int _RoleID = -1;
        #endregion

        #region Private Methods
        void BindingSender(int pUser, int pRoleID, int pStatus)
        {
            int vSkip = int.Parse(lblSkipSender.Value);
            int vPageSize = 0;
            int.TryParse(txtPageSizeSender.Text, out vPageSize);
            dgdSender.DataSource = BuNe.HtmlContent.Instance().GetsStatusAndSender(txtSearch.Text, pUser, pRoleID, pStatus, vSkip, vPageSize);
            Localization.LocalizeDataGrid(ref dgdSender, LocalResourceFile);
            dgdSender.DataBind();
        }
        void BindingReceiver(int pUser, int pRoleID, int pStatus)
        {
            int vSkip = int.Parse(lblSkipReceiver.Value);
            int vPageSize = 0;
            int.TryParse(txtPageSizeReceiver.Text, out vPageSize);
            //pnlReceiver.Visible = pStatus == (int)BuUt.StatusEnums.Sent || pStatus == (int)BuUt.StatusEnums.Returned;
            pnlReceiver.Visible = !(pStatus == (int)BuUt.StatusEnums.Editing) ||
                (pStatus == (int)BuUt.StatusEnums.Approved && BuNe.HtmlContent.Instance().CheckTopRole(pRoleID));
                //|| pStatus == (int)BuUt.StatusEnums.Approved || pStatus == (int)BuUt.StatusEnums.Downed;
            if (pnlReceiver.Visible)
            {
                dgdReceiver.DataSource = BuNe.HtmlContent.Instance().GetsStatusAndReceiver(txtSearch.Text, pUser, pRoleID, pStatus, vSkip, vPageSize);
                Localization.LocalizeDataGrid(ref dgdReceiver, LocalResourceFile);
                dgdReceiver.DataBind();
            }
        }

        void SetMenuItemActive(int pStatus)
        {
            cmdNew.CssClass = cmdNew.CssClass.Replace("MenuItemActive", "");
            cmdEditing.CssClass = cmdEditing.CssClass.Replace("MenuItemActive", "");
            cmdWaiting.CssClass = cmdWaiting.CssClass.Replace("MenuItemActive", "");
            cmdReturned.CssClass = cmdReturned.CssClass.Replace("MenuItemActive", "");
            cmdApproved.CssClass = cmdApproved.CssClass.Replace("MenuItemActive", "");
            cmdDowned.CssClass = cmdDowned.CssClass.Replace("MenuItemActive", "");
            cmdDeleted.CssClass = cmdDeleted.CssClass.Replace("MenuItemActive", "");
            // 
            switch (pStatus)
            {
                case -1:
                    {
                        cmdNew.CssClass = "MenuItemActive " + cmdNew.CssClass;
                        break;
                    }
                case (int)BuUt.StatusEnums.Editing:
                    {
                        cmdEditing.CssClass = "MenuItemActive " + cmdEditing.CssClass;
                        break;
                    }
                case (int)BuUt.StatusEnums.Sent:
                    {
                        cmdWaiting.CssClass = "MenuItemActive " + cmdWaiting.CssClass;
                        break;
                    }
                case (int)BuUt.StatusEnums.Returned:
                    {
                        cmdReturned.CssClass = "MenuItemActive " + cmdReturned.CssClass;
                        break;
                    }
                case (int)BuUt.StatusEnums.Approved:
                    {
                        cmdApproved.CssClass = "MenuItemActive " + cmdApproved.CssClass;
                        break;
                    }
                case (int)BuUt.StatusEnums.Downed:
                    {
                        cmdDowned.CssClass = "MenuItemActive " + cmdDowned.CssClass;
                        break;
                    }
                case (int)BuUt.StatusEnums.Deleted:
                    {
                        cmdDeleted.CssClass = "MenuItemActive " + cmdDeleted.CssClass;
                        break;
                    }
            }
        }

        #endregion

        #region Event Handlers
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Init runs when the control is initialised
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// 	[cnurse]	9/10/2004	Updated to reflect design changes for Help, 508 support
        ///                       and localisation
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            jQuery.RequestDnnPluginsRegistration();

            foreach (DataGridColumn column in dgdSender.Columns)
            {
                if (ReferenceEquals(column.GetType(), typeof(ImageCommandColumn)))
                {
                    //Manage Delete Confirm JS
                    var imageColumn = (ImageCommandColumn)column;
                    imageColumn.Visible = IsEditable;
                    if (imageColumn.CommandName == "Delete")
                    {
                        imageColumn.OnClickJS = Localization.GetString("DeleteItem");
                    }

                    //Manage Edit Column NavigateURLFormatString
                    if (imageColumn.CommandName == "Edit")
                    {
                        //so first create the format string with a dummy value and then
                        //replace the dummy value with the FormatString place holder
                        string formatString = EditUrl("HtmlContentID", "KEYFIELD", "Edit");
                        formatString = formatString.Replace("KEYFIELD", "{0}");
                        imageColumn.NavigateURLFormatString = formatString;
                    }

                    //Localize Image Column Text
                    if (!String.IsNullOrEmpty(imageColumn.CommandName))
                    {
                        imageColumn.Text = Localization.GetString(imageColumn.CommandName, LocalResourceFile);
                    }
                }// if
            }// foreach
        }// Init

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            cmdSearch.Click += cmdSearch_Click;
            //cmdNew.Click += cmdNew_Click;
            cmdEditing.Click += cmdEditing_Click;
            cmdWaiting.Click += cmdWaiting_Click;
            cmdReturned.Click += cmdReturned_Click;
            cmdApproved.Click += cmdApproved_Click;
            cmdDowned.Click += cmdDowned_Click;
            cmdDeleted.Click += cmdDeleted_Click;
            //cmdUploadThemnail.Click += new EventHandler(cmdUploadThemnail_Click);

            // Paging Sender
            lbtPageBeginSender.Click += lbtPageBeginSender_Click;
            lbtPagePreviousSender.Click += lbtPagePreviousSender_Click;
            lbtPageNextSender.Click += lbtPageNextSender_Click;
            lbtPageEndSender.Click += lbtPageEndSender_Click;

            // Paging Receiver
            lbtPageBeginReceiver.Click += lbtPageBeginReceiver_Click;
            lbtPagePreviousReceiver.Click += lbtPagePreviousReceiver_Click;
            lbtPageNextReceiver.Click += lbtPageNextReceiver_Click;
            lbtPageEndReceiver.Click += lbtPageEndReceiver_Click;

            dgdSender.ItemDataBound += OnDataGridSender_ItemDataBound;
            dgdReceiver.ItemDataBound += OnDataGridReceiver_ItemDataBound;
            if (!IsPostBack)
            {
                if ((Request.QueryString["SkipSender"] != null))
                {
                    lblSkipSender.Value = Request.QueryString["SkipSender"];
                }
                if ((Request.QueryString["SkipReceiver"] != null))
                {
                    lblSkipReceiver.Value = Request.QueryString["SkipReceiver"];
                }
               
                if (UserInfo.IsSuperUser)
                    lblManagerVisible.Text = UserId.ToString();
                if ((Request.QueryString["RoleID"] != null))
                {
                    _RoleID = Int32.Parse(Request.QueryString["RoleID"]);
                    ViewState["RoleID"] = _RoleID;
                }
                if (_RoleID == -1)
                {
                    // Set default Role for this User.
                    var Roles = BuNe.HtmlContent.Instance().GetsRole(UserId);
                    if (Roles == null || Roles.Count < 1)
                    {
                        return;
                    }
                    // Get first RoleID.
                    _RoleID = int.Parse(Roles[0].GetValue(0).ToString());
                    ViewState["RoleID"] = _RoleID;
                }
                if ((Request.QueryString["Status"] != null))
                {
                    ViewState["Status"] = Request.QueryString["Status"];
                    int vStatus = int.Parse(ViewState["Status"].ToString());
                    if(MtdLoadPageCountSender(UserId, _RoleID, vStatus) > 0)
                        MtdLoadListItemSender(UserId, _RoleID, vStatus);
                    //BindingSender(UserId, _RoleID, vStatus);
                    if(MtdLoadPageCountReceiver(UserId, _RoleID, vStatus) > 0)
                        MtdLoadListItemReceiver(UserId, _RoleID, vStatus);
                    //BindingReceiver(UserId, _RoleID, vStatus);
                    SetMenuItemActive(vStatus);
                }
                else
                {
                    //cmdEditing_Click(this, e);
                    int vStatus = (int)BuUt.StatusEnums.Editing;
                    ViewState["Status"] = vStatus;
                    if(MtdLoadPageCountSender(UserId, _RoleID, vStatus) > 0)
                        MtdLoadListItemSender(UserId, _RoleID, vStatus);
                    //BindingSender(UserId, _RoleID, vStatus);
                    if(MtdLoadPageCountReceiver(UserId, _RoleID, vStatus) > 0)
                        MtdLoadListItemReceiver(UserId, _RoleID, vStatus);
                    //BindingReceiver(UserId, _RoleID, vStatus);
                    SetMenuItemActive(vStatus);
                }
                cmdNew.OnClientClick = EditUrl("HtmlContentID", "-1", "Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing, "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value) + "; return false;";
            }

            // Release Business Object.
            BuNe.HtmlContent.Release();
        }

        void cmdUploadThemnail_Click(object sender, EventArgs e)
        {
            //fupThemnail
        }

        #region Cmd
        void cmdSearch_Click(object sender, EventArgs e)
        {
            int vStatus = int.Parse(ViewState["Status"].ToString());
            _RoleID = int.Parse(ViewState["RoleID"].ToString());
            if (MtdLoadPageCountSender(UserId, _RoleID, vStatus) > 0)
                MtdLoadListItemSender(UserId, _RoleID, vStatus);
            //BindingSender(UserId, _RoleID, vStatus);
            if (MtdLoadPageCountReceiver(UserId, _RoleID, vStatus) > 0)
                MtdLoadListItemReceiver(UserId, _RoleID, vStatus);
            //BindingReceiver(UserId, _RoleID, vStatus);
            SetMenuItemActive(vStatus);
        }

        void cmdNew_Click(object sender, EventArgs e)
        {
            Response.Redirect(EditUrl("HtmlContentID", "-1", "Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing));
            //txtSearch.Text = string.Empty;
            //Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing));
            //int vStatus = (int)BuUt.StatusEnums.Editing;
            //ViewState["Status"] = vStatus;
            //BindingSender(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //BindingReceiver(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //SetMenuItemActive(vStatus);
        }

        void cmdEditing_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing));
            //txtSearch.Text = string.Empty;
            //int vStatus = (int)BuUt.StatusEnums.Editing;
            //ViewState["Status"] = vStatus;
            //BindingSender(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //BindingReceiver(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //SetMenuItemActive(vStatus);
        }

        void cmdWaiting_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Sent));
            //txtSearch.Text = string.Empty;
            //int vStatus = (int)BuUt.StatusEnums.Sent;
            //ViewState["Status"] = vStatus;
            //BindingSender(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //BindingReceiver(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //SetMenuItemActive(vStatus);
        }

        void cmdReturned_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Returned));
            //txtSearch.Text = string.Empty;
            //int vStatus = (int)BuUt.StatusEnums.Returned;
            //ViewState["Status"] = vStatus;
            //BindingSender(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //BindingReceiver(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //SetMenuItemActive(vStatus);
        }

        void cmdApproved_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Approved));
            //txtSearch.Text = string.Empty;
            //int vStatus = (int)BuUt.StatusEnums.Approved;
            //ViewState["Status"] = vStatus;
            //BindingSender(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //BindingReceiver(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //SetMenuItemActive(vStatus);
        }

        void cmdDowned_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Downed));
            //txtSearch.Text = string.Empty;
            //int vStatus = (int)BuUt.StatusEnums.Downed;
            //ViewState["Status"] = vStatus;
            //BindingSender(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //BindingReceiver(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //SetMenuItemActive(vStatus);
        }

        void cmdDeleted_Click(object sender, EventArgs e)
        {
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Deleted));
            //txtSearch.Text = string.Empty;
            //int vStatus = (int)BuUt.StatusEnums.Deleted;
            //ViewState["Status"] = vStatus;
            //BindingSender(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //BindingReceiver(UserId, int.Parse(ViewState["RoleID"].ToString()), vStatus);
            //SetMenuItemActive(vStatus);
        }
        #endregion Cmd

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        protected void OnDataGridSender_ItemDataBound(object source, DataGridItemEventArgs e)
        {
            var item = e.Item;
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.SelectedItem)
            {
                var imgColumnControl = item.Controls[0].Controls[0];
                if (imgColumnControl is HyperLink)
                {
                    var editLink = (HyperLink)imgColumnControl;
                    editLink.NavigateUrl = EditUrl("HtmlContentID", ((System.Web.UI.WebControls.TableCell)(item.Controls[1])).Text, "Edit", "Status=" + ViewState["Status"], "Sender=true", "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value);
                }
            }
        }

        protected void OnDataGridReceiver_ItemDataBound(object source, DataGridItemEventArgs e)
        {
            var item = e.Item;
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.SelectedItem)
            {
                var imgColumnControl = item.Controls[0].Controls[0];
                if (imgColumnControl is HyperLink)
                {
                    var editLink = (HyperLink)imgColumnControl;
                    editLink.NavigateUrl = EditUrl("HtmlContentID", ((System.Web.UI.WebControls.TableCell)(item.Controls[1])).Text, "Edit", "Status=" + ViewState["Status"], "Sender=false", "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value);
                }
            }
        }
        #endregion Event Handlers

        #region Paging Sender
        int MtdLoadPageCountSender(int pUser, int pRoleID, int pStatus)
        {
            int iCount = BuNe.HtmlContent.Instance().GetCountStatusAndSender(txtSearch.Text, pUser, pRoleID, pStatus);
            dgdSender.Visible = pnlPagingSender.Visible = iCount > 0;
            if (!pnlPagingSender.Visible)
            {
                return 0;
            }
            lblCountSender.Value = iCount.ToString();
            int iPageCount = 0;
            if (iCount > 0)
            {
                // Tính PageCount từ StartIndex và PageSize.
                int iPageSize = 0;
                if (txtPageSizeSender.Text != "")
                    iPageSize = int.Parse(txtPageSizeSender.Text);
                //int iStartIndex = 0;
                //if (lblStartIndex.Value != "")
                //    iStartIndex = int.Parse(lblStartIndex.Value);
                iPageCount = iCount / iPageSize;
                if (iCount % iPageSize > 0)
                    iPageCount++;
                //iPageCount += (iCount - iStartIndex) / iPageSize;
                //if ((iCount - iStartIndex) % iPageSize > 0)
                //    iPageCount++;
            }
            lblPageCountSender.Text = iPageCount.ToString();
            return iCount;
        }
        void MtdLoadListItemSender(int pUser, int pRoleID, int pStatus)
        {
            int iPageSize = 0;
            int.TryParse(txtPageSizeSender.Text, out iPageSize);
            int iPageCount = int.Parse(lblPageCountSender.Text);
            int iSkip = int.Parse(lblSkipSender.Value);
            int iCount = int.Parse(lblCountSender.Value);
            if (iSkip >= iCount && iSkip > 0)
                iSkip -= iPageSize;
            int iPageCurrent = (iSkip + iPageSize) / iPageSize;
            if (iSkip % iPageSize > 0)
                iPageCurrent++;
            // Load Sender.
            dgdSender.DataSource = BuNe.HtmlContent.Instance().GetsStatusAndSender(txtSearch.Text, pUser, pRoleID, pStatus, iSkip, iPageSize);
            Localization.LocalizeDataGrid(ref dgdSender, LocalResourceFile);
            dgdSender.DataBind();
            // Enable Navigator.
            pnlPagingSender.Enabled = (iPageCount > 1) || (iPageCurrent == iPageCount + 1 && iPageCount > 0);
            if (pnlPagingSender.Enabled)
            {
                lbtPageBeginSender.Enabled = lbtPagePreviousSender.Enabled = ((iPageCurrent > 1 && iPageCount > 1) || (iPageCurrent == iPageCount + 1));
                lbtPageEndSender.Enabled = lbtPageNextSender.Enabled = (iPageCurrent < iPageCount && iPageCount > 1);
            }

            lblPageCurrentSender.Text = iPageCurrent.ToString();
            lblEndSender.Visible = iPageCurrent > iPageCount;
            lblPageCurrentSender.Visible = !lblEndSender.Visible;
            //lblPageCurrent.Visible = iPageCurrent <= iPageCount;
        }
        protected void lbtPageBeginSender_Click(object sender, EventArgs e)
        {
            lblSkipSender.Value = "0";
            int iStatus = int.Parse(ViewState["Status"].ToString());
            _RoleID = int.Parse(ViewState["RoleID"].ToString());
            if(MtdLoadPageCountSender(UserId, _RoleID, iStatus) > 0)
                MtdLoadListItemSender(UserId, _RoleID, iStatus);
            cmdNew.OnClientClick = EditUrl("HtmlContentID", "-1", "Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing, "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value) + "; return false;";
        }
        protected void lbtPagePreviousSender_Click(object sender, EventArgs e)
        {
            int iSkip = int.Parse(lblSkipSender.Value);
            int iPageSize = 0;
            int.TryParse(txtPageSizeSender.Text, out iPageSize);
            iSkip -= iPageSize;
            if (iSkip < 0)
                iSkip = 0;
            lblSkipSender.Value = iSkip.ToString();

            int iStatus = int.Parse(ViewState["Status"].ToString());
            _RoleID = int.Parse(ViewState["RoleID"].ToString());
            if (MtdLoadPageCountSender(UserId, _RoleID, iStatus) > 0)
            {
                int iPageCurrent = int.Parse(lblPageCurrentSender.Text);
                if (iPageCurrent > 1)
                    MtdLoadListItemSender(UserId, _RoleID, iStatus);
            }
            cmdNew.OnClientClick = EditUrl("HtmlContentID", "-1", "Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing, "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value) + "; return false;";
        }
        protected void lbtPageNextSender_Click(object sender, EventArgs e)
        {
            int iSkip = int.Parse(lblSkipSender.Value);
            int iPageSize = 0;
            int.TryParse(txtPageSizeSender.Text, out iPageSize);
            int iCount = int.Parse(lblCountSender.Value);
            iSkip += iPageSize;
            if (iSkip >= iCount)
                iSkip--;
            lblSkipSender.Value = iSkip.ToString();

            int iStatus = int.Parse(ViewState["Status"].ToString());
            _RoleID = int.Parse(ViewState["RoleID"].ToString());
            if (MtdLoadPageCountSender(UserId, _RoleID, iStatus) > 0)
            {
                int iPageCurrent = int.Parse(lblPageCurrentSender.Text);
                int iPageCount = int.Parse(lblPageCountSender.Text);
                if (iPageCurrent < iPageCount)
                    MtdLoadListItemSender(UserId, _RoleID, iStatus);
            }
            cmdNew.OnClientClick = EditUrl("HtmlContentID", "-1", "Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing, "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value) + "; return false;";
        }
        protected void lbtPageEndSender_Click(object sender, EventArgs e)
        {
            int iSkip = int.Parse(lblSkipSender.Value);
            int iPageSize = 0;
            int.TryParse(txtPageSizeSender.Text, out iPageSize);
            int iCount = int.Parse(lblCountSender.Value);
            while ((iCount - iSkip) > iPageSize)
                iSkip += iPageSize;
            lblSkipSender.Value = iSkip.ToString();

            int iStatus = int.Parse(ViewState["Status"].ToString());
            _RoleID = int.Parse(ViewState["RoleID"].ToString());
            if(MtdLoadPageCountSender(UserId, _RoleID, iStatus) > 0)
                MtdLoadListItemSender(UserId, _RoleID, iStatus);
            cmdNew.OnClientClick = EditUrl("HtmlContentID", "-1", "Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing, "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value) + "; return false;";
        }
        #endregion Paging Sender

        #region Paging Receiver
        int MtdLoadPageCountReceiver(int pUser, int pRoleID, int pStatus)
        {
            int iCount = BuNe.HtmlContent.Instance().GetCountStatusAndReceiver(txtSearch.Text, pUser, pRoleID, pStatus);
            dgdReceiver.Visible = pnlPagingReceiver.Visible = iCount > 0;
            if (!pnlPagingReceiver.Visible)
            {
                return 0;
            }
            lblCountReceiver.Value = iCount.ToString();
            int iPageCount = 0;
            if (iCount > 0)
            {
                // Tính PageCount từ StartIndex và PageSize.
                int iPageSize = 0;
                if (txtPageSizeReceiver.Text != "")
                    iPageSize = int.Parse(txtPageSizeReceiver.Text);
                //int iStartIndex = 0;
                //if (lblStartIndex.Value != "")
                //    iStartIndex = int.Parse(lblStartIndex.Value);
                iPageCount = iCount / iPageSize;
                if (iCount % iPageSize > 0)
                    iPageCount++;
                //iPageCount += (iCount - iStartIndex) / iPageSize;
                //if ((iCount - iStartIndex) % iPageSize > 0)
                //    iPageCount++;
            }
            lblPageCountReceiver.Text = iPageCount.ToString();
            return iCount;
        }
        void MtdLoadListItemReceiver(int pUser, int pRoleID, int pStatus)
        {
            int iPageSize = 0;
            int.TryParse(txtPageSizeReceiver.Text, out iPageSize);
            int iPageCount = int.Parse(lblPageCountReceiver.Text);
            int iSkip = int.Parse(lblSkipReceiver.Value);
            int iCount = int.Parse(lblCountReceiver.Value);
            if (iSkip >= iCount && iSkip > 0)
                iSkip -= iPageSize;
            int iPageCurrent = (iSkip + iPageSize) / iPageSize;
            if (iSkip % iPageSize > 0)
                iPageCurrent++;
            // Load Receiver.
            dgdReceiver.DataSource = BuNe.HtmlContent.Instance().GetsStatusAndReceiver(txtSearch.Text, pUser, pRoleID, pStatus, iSkip, iPageSize);
            Localization.LocalizeDataGrid(ref dgdReceiver, LocalResourceFile);
            dgdReceiver.DataBind();
            // Enable Navigator.
            pnlPagingReceiver.Enabled = (iPageCount > 1) || (iPageCurrent == iPageCount + 1 && iPageCount > 0);
            if (pnlPagingReceiver.Enabled)
            {
                lbtPageBeginReceiver.Enabled = lbtPagePreviousReceiver.Enabled = ((iPageCurrent > 1 && iPageCount > 1) || (iPageCurrent == iPageCount + 1));
                lbtPageEndReceiver.Enabled = lbtPageNextReceiver.Enabled = (iPageCurrent < iPageCount && iPageCount > 1);
            }

            lblPageCurrentReceiver.Text = iPageCurrent.ToString();
            lblEndReceiver.Visible = iPageCurrent > iPageCount;
            lblPageCurrentReceiver.Visible = !lblEndReceiver.Visible;
            //lblPageCurrent.Visible = iPageCurrent <= iPageCount;
        }
        protected void lbtPageBeginReceiver_Click(object sender, EventArgs e)
        {
            lblSkipReceiver.Value = "0";
            int iStatus = int.Parse(ViewState["Status"].ToString());
            _RoleID = int.Parse(ViewState["RoleID"].ToString());
            if(MtdLoadPageCountReceiver(UserId, _RoleID, iStatus) > 0)
                MtdLoadListItemReceiver(UserId, _RoleID, iStatus);
            cmdNew.OnClientClick = EditUrl("HtmlContentID", "-1", "Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing, "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value) + "; return false;";
        }
        protected void lbtPagePreviousReceiver_Click(object sender, EventArgs e)
        {
            int iSkip = int.Parse(lblSkipReceiver.Value);
            int iPageSize = 0;
            int.TryParse(txtPageSizeReceiver.Text, out iPageSize);
            iSkip -= iPageSize;
            if (iSkip < 0)
                iSkip = 0;
            lblSkipReceiver.Value = iSkip.ToString();

            int iStatus = int.Parse(ViewState["Status"].ToString());
            _RoleID = int.Parse(ViewState["RoleID"].ToString());
            if (MtdLoadPageCountReceiver(UserId, _RoleID, iStatus) > 0)
            {
                int iPageCurrent = int.Parse(lblPageCurrentReceiver.Text);
                if (iPageCurrent > 1)
                    MtdLoadListItemReceiver(UserId, _RoleID, iStatus);
            }
            cmdNew.OnClientClick = EditUrl("HtmlContentID", "-1", "Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing, "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value) + "; return false;";
        }
        protected void lbtPageNextReceiver_Click(object sender, EventArgs e)
        {
            int iSkip = int.Parse(lblSkipReceiver.Value);
            int iPageSize = 0;
            int.TryParse(txtPageSizeReceiver.Text, out iPageSize);
            int iCount = int.Parse(lblCountReceiver.Value);
            iSkip += iPageSize;
            if (iSkip >= iCount)
                iSkip--;
            lblSkipReceiver.Value = iSkip.ToString();

            int iStatus = int.Parse(ViewState["Status"].ToString());
            _RoleID = int.Parse(ViewState["RoleID"].ToString());
            if (MtdLoadPageCountReceiver(UserId, _RoleID, iStatus) > 0)
            {
                int iPageCurrent = int.Parse(lblPageCurrentReceiver.Text);
                int iPageCount = int.Parse(lblPageCountReceiver.Text);
                if (iPageCurrent < iPageCount)
                    MtdLoadListItemReceiver(UserId, _RoleID, iStatus);
            }
            cmdNew.OnClientClick = EditUrl("HtmlContentID", "-1", "Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing, "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value) + "; return false;";
        }
        protected void lbtPageEndReceiver_Click(object sender, EventArgs e)
        {
            int iSkip = int.Parse(lblSkipReceiver.Value);
            int iPageSize = 0;
            int.TryParse(txtPageSizeReceiver.Text, out iPageSize);
            int iCount = int.Parse(lblCountReceiver.Value);
            while ((iCount - iSkip) > iPageSize)
                iSkip += iPageSize;
            lblSkipReceiver.Value = iSkip.ToString();

            int iStatus = int.Parse(ViewState["Status"].ToString());
            _RoleID = int.Parse(ViewState["RoleID"].ToString());
            if(MtdLoadPageCountReceiver(UserId, _RoleID, iStatus) > 0)
                MtdLoadListItemReceiver(UserId, _RoleID, iStatus);
            cmdNew.OnClientClick = EditUrl("HtmlContentID", "-1", "Edit", "RoleID=" + ViewState["RoleID"], "Status=" + (int)BuUt.StatusEnums.Editing, "SkipSender=" + lblSkipSender.Value, "SkipReceiver=" + lblSkipReceiver.Value) + "; return false;";
        }
        #endregion Paging Receiver

        /*   
        #region IActionable Members
        public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get
            {

                ModuleActionCollection actions = new ModuleActionCollection();
                ModuleAction action = new ModuleAction(ModuleContext.GetNextActionID());
                action.Title = Localization.GetString("ActionAdd", LocalResourceFile);
                action.CommandName = "";
                action.CommandArgument = "";
                action.Url = EditUrl("Status", "" + (int)BuUt.StatusEnums.Editing);
                action.UseActionEvent = true;
                action.Secure = SecurityAccessLevel.Edit;
                action.Icon = "add.gif";
                actions.Add(action);
                return actions;
            }
        }

        #endregion
*/
    }
}